#!/usr/bin/python3

# Extract time. vsi, asi, alt, rpm data and plot with Gnuplot.
#
# Usage:
# $ ./flight.py <data-file> > data
# $ # delete some garbage data using text utils
# $ gnuplot
# gnuplot> set style data lines
# gnuplot> set xdata time
# gnuplot> set timefmt "%H:%M:%S"
# gnuplot> plot 'data' using 1:2, '' using 1:3, '' using 1:4, '' using 1:5

if __name__ == '__main__':
	import sys

	dn = ['clock-indicated', 'vsi-fpm', 'asi-kt', 'alt-ft', 'engine-rpm']
	ds = 0
	cs = '61' # an invalid second
	try:
		f = open(sys.argv[1], mode = 'r')
		print('#' + ' '.join(dn))
		for l in f:
			t = l.rstrip('\n').split(',')
			t = [i.split('=') for i in t]
			t = [i[1] for n in dn for i in t if i[0] == n]
			ns = t[0].split(':')[2]
			if cs != ns:
				# a new second, so reset ds and update cs
				ds = 0
				cs = ns
			t[0] = t[0] + '.%02d' % (ds // 1)
			ds += 4.75
			print(' '.join(t))
	except Exception:
		exit(1)
	finally:
		f.close()

	exit(0)
